﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Code;
using ZFine.Domain.Entity.SystemManage;
using ZFine.Domain.IRepository.SystemManage;
using ZFine.Repository.SystemManage;
using System.Collections.Generic;
using System.Linq;
using System.Data.Common;
using System.Text;
using System.Data.SqlClient;

namespace ZFine.Application.SystemManage
{
    public class ItemsDetailApp
    {
        private IItemsDetailRepository service = new ItemsDetailRepository();

        public List<ItemsDetailEntity> GetList(string itemId = "", string keyword = "")
        {
            var expression = ExtLinq.True<ItemsDetailEntity>();
            if (!string.IsNullOrEmpty(itemId))
            {
                expression = expression.And(t => t.F_ItemId == itemId);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_ItemName.Contains(keyword));
                expression = expression.Or(t => t.F_ItemCode.Contains(keyword));
            }
            return service.IQueryable(expression).OrderBy(t => t.F_SortCode).ToList();
        }
        public List<ItemsDetailEntity> GetItemList(string enCode)
        {
            return service.GetItemList(enCode);
        }
        public ItemsDetailEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(ItemsDetailEntity itemsDetailEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                itemsDetailEntity.Modify(keyValue);
                service.Update(itemsDetailEntity);
            }
            else
            {
                itemsDetailEntity.Create();
                service.Insert(itemsDetailEntity);
            }
        }

        // 取得可用学校的年级列表
        public List<ItemsDetailEntity> GetEnableClassList(string enCode, string schoolId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT DISTINCT d.*
                              FROM SC_Class c, Sys_Items i,Sys_ItemsDetail d
                             WHERE 1=1
                               AND i.F_Id = d.F_ItemId
                               AND i.F_EnCode =  @enCode
                               AND d.F_ItemCode = c.Class
                               AND c.SchoolId = @schoolId
                               AND c.isEnable =1");
            DbParameter[] parameter ={
                    new SqlParameter("@enCode",enCode),
                    new SqlParameter("@schoolId",schoolId),
                };
            return service.FindList(strSql.ToString(), parameter);
        }

        // 取得已选择的年级
        public string GetSelectedClass(string enCode, string classValue)
        {
            string strClassName = "";
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT DISTINCT d.*
                              FROM Sys_Items i,Sys_ItemsDetail d
                             WHERE 1=1
                               AND i.F_Id = d.F_ItemId
                               AND i.F_EnCode =  @enCode
                               AND d.F_ItemCode = @classValue");
            DbParameter[] parameter ={
                    new SqlParameter("@enCode",enCode),
                    new SqlParameter("@classValue",classValue),
                };
            List<ItemsDetailEntity> itemsDetailList = service.FindList(strSql.ToString(), parameter);
            if(null != itemsDetailList && 0 < itemsDetailList.Count)
            {
                strClassName = itemsDetailList[0].F_ItemName;
            }
            return strClassName;
        }
    }
}
